/**
 * 武汉天瑞兴科技有限公司
 */
package com.adds.lvds.controller.dict;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adds.lvds.core.utils.CommUtils;
import com.adds.lvds.model.dict.SysDicttype;
import com.adds.lvds.service.impl.dict.DictInfoService;

/**
 * 项目名称 ：adds-web-server
 * 
 * @author zhangjian
 * @time 2018-3-15下午4:03:42
 */
@Controller
@RequestMapping("/dict/")
public class DictInfoController {
	@Inject
	private DictInfoService dictInfoService;

	/**
	 * 首页初始化
	 * 
	 * @return
	 */
	@RequestMapping(value = "index")
	public String index() {
		return "/dict/dict_info_main";
	}

	/**
	 * 异步加载首页数据
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "execute")
	public List<Object> execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {// 4个格式
		String dictid = request.getParameter("id");
		List<Object> list = dictInfoService.getDictJson(dictid);
		return list;
	}

	// 录入字典数据
	@ResponseBody
	@RequestMapping(value="addDict")
	public Map<String, Object> addDict(
			@Param(value = "taw_system_dicttype") SysDicttype sysDicttype,
			HttpServletResponse response) {

		Map<String, Object> map = dictInfoService
				.insertSysDicttype(sysDicttype);
		sysDicttype = (SysDicttype) map
				.get("taw_system_dicttype");
		String result = (String) map.get("result");
		String msg = "";
		switch (result) {
		case CommUtils.SUCC:
			msg = "录入字典数据成功！";
			break;
		case CommUtils.ERROR:
			msg = "录入字典数据失败！";
			break;
		}
		map.put("data", sysDicttype);
		map.put("errcode", result);
		map.put("errmsg", msg);
		return map;
	}

	// 跳转编辑页面
	@RequestMapping(value="editDict")
	public String editDict(@Param(value = "dictid") String dictid, Model model) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("dictid", dictid);
		SysDicttype sysDicttype = dictInfoService
				.querySysDicttypeByMap(map);
		model.addAttribute("taw_system_dicttype", sysDicttype);
		return "/dict/dict_info_edit";
	}

	// 录入字典数据
	@ResponseBody
	@RequestMapping(value="doEditDict")
	public Map<String, Object> doEditDict(
			@Param(value = "taw_system_dicttype") SysDicttype sysDicttype,
			HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		String result = dictInfoService
				.updateSysDicttype(sysDicttype);
		String msg = "";
		switch (result) {
		case CommUtils.SUCC:
			msg = "编辑字典数据成功！";
			break;
		case CommUtils.ERROR:
			msg = "编辑字典数据失败！";
			break;
		}
		map.put("errcode", result);
		map.put("errmsg", msg);
		return map;
	}
	//删除字典数据
	@ResponseBody
	@RequestMapping(value="delDict")
	public Map<String, Object> delDict(
			@Param(value = "dictid") String dictid,
			HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("dictid", dictid);
		List<SysDicttype> list=dictInfoService.findChildByMap(map);
		String result = dictInfoService.deleteSysDicttypeByMap(list);
		String msg = "";
		switch (result) {
		case CommUtils.SUCC:
			msg = "删除["+list.size()+"]条字典数据成功！";
			break;
		case CommUtils.ERROR:
			msg = "删除字典数据失败！";
			break;
		}
		map.put("errcode", result);
		map.put("errmsg", msg);
		return map;
	}
	//
	// 查看字典数据页面
	@RequestMapping(value="detailDict")
	public String detailDict(@Param(value = "dictid") String dictid, Model model) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("dictid", dictid);
		SysDicttype sysDicttype = dictInfoService
				.querySysDicttypeByMap(map);
		model.addAttribute("taw_system_dicttype", sysDicttype);
		return "/dict/dict_info_detail";
	}
}
